package com.bxy.dzsc.controller;

import com.bxy.dzsc.utilbeans.PersonInfoForm;
import com.bxy.dzsc.utilbeans.RegisterForm;
import com.bxy.dzsc.utilbeans.UserLoginForm;
import com.bxy.dzsc.domain.Users;
import com.bxy.dzsc.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class UsersController {
    @Autowired
    private UsersService usersService;
    @RequestMapping("/userLogin")
    @ResponseBody
    public String userlogin(Model model, HttpSession session, @RequestBody UserLoginForm userLoginForm){
        String uname=userLoginForm.getUsername();
        String pwd=userLoginForm.getPassword();
        System.out.println(uname);
        Users user=usersService.findbynameandpwd(uname,pwd);
        if (user!=null){
            session.setAttribute("user",user);
            System.out.println("成功查询到用户");
            String node=user.getUsername();
            model.addAttribute("node",node);
            System.out.println(model.getAttribute("node"));
            return node;
        }else{
            System.out.println("未查找到用户");
            String node="error";
            return node;
        }
    }
    @RequestMapping("/register")
    @ResponseBody
    public ModelAndView userregister(@RequestBody RegisterForm registerForm){
        String username=registerForm.getUsername();
        String password=registerForm.getPassword();
        Users findbyname = usersService.findbyname(username);
        if (findbyname ==null){
            Users users=new Users();
            users.setUsername(username);
            users.setPassword(password);
            usersService.insertuser(users);
            System.out.println("注册成功");
            String node=username;
            ModelAndView mv=new ModelAndView("index");
            return mv;
        }else {
            System.out.println("注册失败，用户名重复");
            String node=null;
            ModelAndView mv=new ModelAndView("index");
            return mv;
        }
    }
    @RequestMapping("/updataPersonInfo")
    @ResponseBody
    public String updateuser (HttpSession session,@RequestBody PersonInfoForm personInfoForm) throws ParseException {
        String username = personInfoForm.getUsername();
        String password = personInfoForm.getPassword();
        Integer age=personInfoForm.getAge();
        String birthPlace=personInfoForm.getBirthPlace();
        Date birthDate=personInfoForm.getBirthDate();
        System.out.println(birthDate);
        List<String> pinwei=personInfoForm.getPinwei();
        String pin="";
        for (String s : pinwei) {
            pin=pin+","+s;
        }
        Integer gen=3;
        String gender=personInfoForm.getGender();
        if (gender.equals("男")){
            gen=0;
        }else{
            gen=1;
        }
        String experience=personInfoForm.getExperience();
        String desc=personInfoForm.getDesc();
        Users users=new Users();
        users.setUsername(username);
        users.setPassword(password);
        users.setAge(age);
        users.setBirthPlace(birthPlace);
        users.setBirthDate(birthDate);
        users.setGender(gen);
        users.setExperience(experience);
        users.setDess(desc);
        users.setPinwei(pin);
        Users user = (Users) session.getAttribute("user");
        if (user==null){
            System.out.println("用户未登录");
        }else{
            users.setId(user.getId());
            usersService.updateuser(users);
            System.out.println("修改成功");
        }
        return username;
    }
}
